package me.xiaoyan.android.tuition.activity;

import org.json.JSONArray;
import org.json.JSONException;

import me.xiaoyan.android.net.AsyncImageLoader;
import me.xiaoyan.android.net.AsyncImageLoader.ImageCallback;
import me.xiaoyan.android.tuition.R;
import me.xiaoyan.android.tuition.utils.AppManager;
import me.xiaoyan.android.widget.BaseUtil;
import me.xiaoyan.android.widget.YLog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

public class Orders extends Common {
	private static final String TAG = "Activity.Orders";
	private LinearLayout lv_scroll_box = null;
	private AsyncImageLoader imgLoader = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_common_scroller);
		super.setPageTitle(getString(R.string.nav_my_order));
		// 判断数据是否正常
		if (!AppManager.getInstance().hasAppDatas(ORDER_INFO)
				|| null == AppManager.getInstance().getAppDatas(ORDER_INFO)) {
			showToast(getString(R.string.no_pay_nothing), Toast.LENGTH_SHORT);
			return;
		}
		imgLoader = new AsyncImageLoader();
		lv_scroll_box = (LinearLayout) findViewById(R.id.lv_scroll_box);
		JSONArray json = (JSONArray) AppManager.getInstance().getAppDatas(
				ORDER_INFO);
		// 计算边距
		int bottonMargin = BaseUtil.dip2px(this, 10);
		// 获取动画
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.fadein);
		for (int i = 0; i < json.length(); i++) {
			final ImageView img = new ImageView(this);
			img.setAnimation(anim);
			LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			params.setMargins(bottonMargin, bottonMargin, bottonMargin, 0);
			params.gravity = Gravity.CENTER_HORIZONTAL;
			img.setLayoutParams(params);
			try {
				imgLoader.getBitMap(json.getJSONObject(i).optString("url"),
						new ImageCallback() {

							@Override
							public void imageLoaded(Bitmap drawable) {
								img.setImageBitmap(drawable);
							}
						});
			} catch (JSONException e) {
				YLog.e(TAG, e);
			}
			lv_scroll_box.addView(img);
		}
	}

	@Override
	public void onBackPressed() {
		super.goback();
	}
}
